<?php

namespace app\common\business;

use app\common\lib\Arr;
use app\common\model\mysql\ScreenFunding;
use app\common\model\mysql\ScreenTalent;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;

class Funding {

    public $model = null;

    public function __construct()
    {
        $this->model = new ScreenFunding();
    }

    /**
     * 获取分页列表
     * @throws Exception
     */
    public function getTableList($page=null,$limit=null): array
    {
        $data=$this->model->getTableList([],$page,$limit);
        $data['list']=self::resetList($data['list']);
        return $data;
    }

    /**
     * 新增/保存数据
     * @param $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException|Exception
     */
    public function save($data): bool
    {
        if(empty($data['id'])){
            unset($data['id']);
        }
        return $this->model->saveData($data);
    }

    /**
     * 根据id删除
     * @param $id
     * @return bool
     * @throws Exception
     */
    public function deleteById($id): bool
    {
        if(empty($id)){
            throw new Exception('参数错误');
        }
        return $this->model->deleteById($id);
    }

    /**
     * 根据id修改状态
     * @param $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function updateStatus($data): bool
    {
        if(empty($data['id'])){
            throw new Exception('参数错误');
        }
        return $this->model->updateStatus($data);
    }

    public static function resetList($list){
        if(!empty($list)){
            $category=(new Category())->categoryDictionaries();
            foreach ($list as $k=>$v){
                if(!empty($v['area'])){
                    $list[$k]['area_name']=$category[$v['area']]??'-';
                }
            }
        }
        return $list;
    }
}
